using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Animoso.Domain;
using Animoso.Domain.Data;

namespace Animoso.Web.Controllers
{
    public class SponsorController : ApplicationController
    {
        private ISponsorRepository _sponsorRepository;

        public SponsorController(ISponsorRepository sponsorRepository)
        {
            _sponsorRepository = sponsorRepository;
        }

        public ActionResult Index()
        {
            _sponsorRepository.DeleteInactive();
            var sponsors = _sponsorRepository.GetAll();
            return View(sponsors);
        }

        public ActionResult Create()
        {
            Sponsor sponsor = new Sponsor();
            sponsor.Active = false;
            _sponsorRepository.Add(sponsor);

            SetImageLocationsListItem();
            return View("Edit", sponsor);
        }

        public ActionResult Edit(int id)
        {
            Sponsor sponsor = _sponsorRepository.GetById(id);

            SetImageLocationsListItem();
            return View(sponsor);
        }

        private void SetImageLocationsListItem()
        {
            IEnumerable<ImageLocation> imageLocations = Enum.GetValues(typeof (ImageLocation)).Cast<ImageLocation>();
            IList<SelectListItem> selectListItems = imageLocations.Select(imageLocation => new SelectListItem() {Text = imageLocation.ToString(), Value = imageLocation.ToString()}).ToList();

            ViewData["ImageLocationList"] = selectListItems;
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Sponsor sponsor)
        {
            if (!ModelState.IsValid)
            {
                SetImageLocationsListItem();
                return View(sponsor);
            }

            Sponsor oldSponsor = _sponsorRepository.GetById(sponsor.Id);
            if (!string.IsNullOrEmpty(oldSponsor.Picture))
            {
                sponsor.Picture = oldSponsor.Picture;
            }

            sponsor.Active = true;
            _sponsorRepository.Save(sponsor);
            return RedirectToAction("Index");
        }

        private string MapPath(string fileName)
        {
            return Server.MapPath("\\Upload\\" + Folders.SPONSOR_PICTURE_FOLDER) + "\\" + fileName;
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public string Upload(HttpPostedFileBase FileData, int sponsorId)
        {
            Sponsor sponsor = _sponsorRepository.GetById(sponsorId);

            if (!string.IsNullOrEmpty(sponsor.Picture))
            {
                System.IO.File.Delete(MapPath(sponsor.Picture));
            }

            Guid pictureGuid = Guid.NewGuid();
            string fileName = pictureGuid.ToString() + "." +
                               FileData.FileName.Split('.').Last();

            string pathToImg = MapPath(fileName);

            FileData.SaveAs(pathToImg);

            sponsor.Picture = fileName;
            _sponsorRepository.Save(sponsor);

            return fileName;
        }

        public JsonResult Delete(int sponsorId)
        {
            Sponsor sponsor = _sponsorRepository.GetById(sponsorId);
            if (!string.IsNullOrEmpty(sponsor.Picture))
            {
                System.IO.File.Delete(MapPath(sponsor.Picture));
            }
            _sponsorRepository.Delete(sponsor);

            return Json(true, JsonRequestBehavior.AllowGet);
        }
    }
}
